﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.BLL;
using Code.Models;
using Company.Web.Commen;
using Company.Web.Areas.Manage.Models.SystemModels;

namespace Company.Web.Areas.Manage.Controllers
{
    public class SystemController : ControllerBase
    {
        SystemManage SM = new SystemManage();
        public ActionResult Index()
        {
            var model = new IndexModel
            {
                Site_Info_Address_Ch = CommenManage.Options[OptionKey.Site_Info_Address_Ch].Value,
                Site_Info_Address_En = CommenManage.Options[OptionKey.Site_Info_Address_En].Value,
                Site_Info_CompanyName_Ch = CommenManage.Options[OptionKey.Site_Info_CompanyName_Ch].Value,
                Site_Info_CompanyName_En = CommenManage.Options[OptionKey.Site_Info_CompanyName_En].Value,
                Site_Info_Email = CommenManage.Options[OptionKey.Site_Info_Email].Value,
                Site_Info_Fax = CommenManage.Options[OptionKey.Site_Info_Fax].Value,
                Site_Info_Tel = CommenManage.Options[OptionKey.Site_Info_Tel].Value,
                Site_Info_Zip = CommenManage.Options[OptionKey.Site_Info_Zip].Value,
                Site_Home_IndexImgs_Ch = CommenManage.Options[OptionKey.Site_Home_IndexImgs_Ch].Value,
                Site_Home_IndexImgs_En = CommenManage.Options[OptionKey.Site_Home_IndexImgs_En].Value,
                Site_Info_KeyWord_Ch = CommenManage.Options[OptionKey.Site_Info_KeyWord_Ch].Value,
                Site_Info_KeyWord_En = CommenManage.Options[OptionKey.Site_Info_KeyWord_En].Value,
            };

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(IndexModel model)
        {
            if (ModelState.IsValid)
            {
                List<Option> ops = new List<Option>()
                {
                    new Option{Key=OptionKey.Site_Info_Address_Ch.ToString(),Value=model.Site_Info_Address_Ch},
                    new Option{Key=OptionKey.Site_Info_Address_En.ToString(),Value=model.Site_Info_Address_En},
                    new Option{Key=OptionKey.Site_Info_CompanyName_Ch.ToString(),Value=model.Site_Info_CompanyName_Ch},
                    new Option{Key=OptionKey.Site_Info_CompanyName_En.ToString(),Value=model.Site_Info_CompanyName_En},
                    new Option{Key=OptionKey.Site_Info_Email.ToString(),Value=model.Site_Info_Email},
                    new Option{Key=OptionKey.Site_Info_Fax.ToString(),Value=model.Site_Info_Fax},
                    new Option{Key=OptionKey.Site_Info_Tel.ToString(),Value=model.Site_Info_Tel},
                    new Option{Key=OptionKey.Site_Info_Zip.ToString(),Value=model.Site_Info_Zip},
                    new Option{Key=OptionKey.Site_Home_IndexImgs_Ch.ToString(),Value=model.Site_Home_IndexImgs_Ch},
                    new Option{Key=OptionKey.Site_Home_IndexImgs_En.ToString(),Value=model.Site_Home_IndexImgs_En},
                    new Option{Key=OptionKey.Site_Info_KeyWord_Ch.ToString(),Value=model.Site_Info_KeyWord_Ch},
                    new Option{Key=OptionKey.Site_Info_KeyWord_En.ToString(),Value=model.Site_Info_KeyWord_En},
                };
                int rs = SM.Edit(ops.ToArray());
                string msg = null;
                if (rs > 0)
                {
                    msg = "更新成功";
                    CommenManage.LoadOptions();
                }
                else
                    msg = "操作失败，请稍后再试";
                ViewData["msg"] = msg.AlertOffPage();
            }

            return View(model);
        }


        public ActionResult Edit(ContentType ID)
        {
            var model = new EditModel { ID = ID };
            var lg = (Enum.GetValues(typeof(Language)) as Language[]);
            var key = "Site_Html_" + ID;
            var data=SM.GetTable<Option>().Where(p => p.Key.StartsWith(key)).ToArray();
            model.Info = lg.Select(p => new OptionEditItem { Language = p, Value = data.Where(p1 => p1.Key == "Site_Html_" + ID + "_" + p).Select(p1 => p1.Value).FirstOrDefault() ?? string.Empty }).ToArray();
            return View(model);
        
        }

        [HttpPost,ValidateInput(false)]
        public ActionResult Edit(EditModel model)
        {
            var ops=model.Info.Select(p => new Option { Key = "Site_Html_" + model.ID + "_" + p.Language, Value = p.Value }).ToArray();
            int rs = SM.Edit(ops);
            string msg = null;
            if (rs > 0)
            {
                msg = "更新成功";
                CommenManage.LoadOptions();
            }
            else
                msg = "操作失败，请稍后再试";
            ViewData["msg"] = msg.AlertOffPage();
            return View(model);
        }

        public ActionResult HomeShow()
        {

            return View();
        }

        public ActionResult ServerInfo()
        {

            return View();
        }
    }
}
